using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library;
using NCS.Library.Components.Security;
using NCS.Library.Components.Users;
using NCS.Library.Utilities;
using NCS.Library.Components.Portal;
using System.IO;

namespace NCS.Portal.Administrator.Portals
{
    public partial class PortalLogingManager : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["DropDownPageItems"] = 10;
            }
        }

        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            DataTable tbl;
            tbl = Portal_LogingController.GetLogs(this.txtKeyword.Text, this.dropSearchBy.SelectedValue);
            string tempath = Path.GetTempFileName();
            StreamWriter stream = File.CreateText(tempath);

            if (tbl != null && tbl.Rows.Count > 0)
            {
                for (int i = 0; i < tbl.Rows.Count; i++)
                {
                    stream.WriteLine();
                    string str = tbl.Rows[i]["Actor"].ToString();
                    stream.Write(str);
                    for (int j = 0; j < 50 - str.Length; j++)
                    {
                        stream.Write(".");
                    }
                    stream.Write(" / ");
                    str = tbl.Rows[i]["Date"].ToString();
                    stream.Write(str);
                    for (int j = 0; j < 50 - str.Length; j++)
                    {
                        stream.Write(".");
                    }
                    stream.Write(" / ");
                    str = tbl.Rows[i]["ModuleName"].ToString();
                    stream.Write(str);
                    for (int j = 0; j < 50 - str.Length; j++)
                    {
                        stream.Write(".");
                    }
                    stream.Write(" / ");
                    str = tbl.Rows[i]["Log_Content"].ToString();
                    stream.Write(str);                    
                }
            }

            stream.Flush();
            stream.Close();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "text/txt";
            Response.AppendHeader("content-disposition", "attachment;filename=log.txt");
            Response.WriteFile(tempath);
            Response.End();

        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            DataTable dtUsers;
            dtUsers = Portal_LogingController.GetLogs(this.txtKeyword.Text, this.dropSearchBy.SelectedValue);
            if (!CurrentUser.IsAdmin && !CurrentUser.IsSuperAdmin)
            {
                this.btnDeleteSelected.Visible = false;
            }

            //-------sort----------
            DataView dv = new DataView(dtUsers);
            if (ViewState["sortExpression"] != null && ViewState["sortExpression"].ToString() != string.Empty)
            {
                dv.Sort = ViewState["sortExpression"].ToString();
            }

            if (ViewState["sortDirection"] != null && ViewState["sortDirection"].ToString() == "Desc")
            {
                dv.Sort = ViewState["sortExpression"].ToString() + " " + ViewState["sortDirection"].ToString();
            }
            //--------sort-------------

            dtgUsers.DataSource = dv;
            dtgUsers.DataBind();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            Portal_LogingController.Delete();
        }
        protected void dtgUsers_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["sortDirection"] == null || ViewState["sortDirection"].ToString() == "")
            {
                ViewState["sortDirection"] = "Desc";
            }
            else if (ViewState["sortDirection"].ToString() == "Desc")
            {
                ViewState["sortDirection"] = "";
            }
            ViewState["sortExpression"] = e.SortExpression;

        }
        protected void dtgUsers_PreRender(object sender, EventArgs e)
        {
            if (dtgUsers.BottomPagerRow != null)
            {
                ((Label)dtgUsers.BottomPagerRow.FindControl("lbTotalPages")).Text = dtgUsers.PageCount.ToString();

                //This makes the first and last button disappear when on the first and last pages.
                ((LinkButton)dtgUsers.BottomPagerRow.FindControl("lbtnFirst")).Visible = dtgUsers.PageIndex != 0;
                ((LinkButton)dtgUsers.BottomPagerRow.FindControl("lbtnLast")).Visible = dtgUsers.PageCount != (dtgUsers.PageIndex + 1);

                DropDownList ddlistPageof = (DropDownList)dtgUsers.BottomPagerRow.FindControl("ddlPageItems");
                ddlistPageof.SelectedIndex = ddlistPageof.Items.IndexOf(ddlistPageof.Items.FindByValue(ViewState["DropDownPageItems"].ToString()));
                DropDownList ddlistPageno = (DropDownList)dtgUsers.BottomPagerRow.FindControl("ddlNumberPages");
                ddlistPageno.Items.Clear();
                for (int i = 1; i <= dtgUsers.PageCount; i++)
                {
                    ListItem liTemp = new ListItem();
                    liTemp.Text = i.ToString();
                    liTemp.Value = i.ToString();
                    ddlistPageno.Items.Add(liTemp);
                }
                if (dtgUsers.PageCount != 0)
                {
                    ddlistPageno.SelectedIndex = dtgUsers.PageIndex;
                    ddlistPageno.DataBind();
                }
            }
        }
        protected void ddlNumberPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlistPageno = (DropDownList)dtgUsers.BottomPagerRow.FindControl("ddlNumberPages");
            dtgUsers.PageIndex = Convert.ToInt32(ddlistPageno.SelectedValue) - 1;

        }
        protected void dtgUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dtgUsers.PageIndex = e.NewPageIndex;
        }
        protected void ddlPageItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pageItems = Convert.ToInt32(((DropDownList)sender).SelectedValue);
            this.dtgUsers.PageSize = pageItems;
            ViewState["DropDownPageItems"] = pageItems;
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }
    }
}